//
//  AppDelegate.m
//  GiftItems
//
//  Created by lanou3g on 15/9/18.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseTabBarViewController.h"
#import "UMSocial.h"
#import "APPGuidView.h"
@interface AppDelegate ()<UIScrollViewDelegate>
@property(nonatomic, strong) APPGuidView *guideView;
@property(nonatomic, strong) UIPageControl *pageControl;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [UMSocialData setAppKey:@"55fd0ce3e0f55a305b002070"];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self judgeExistOfFile];
    [self addPageControlForGuideView];
    //判断是否进行本地推送
    

    
    //判断是否进行本地推送
    
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        //注册我们推送的声音、角标、提醒
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
        
        [application registerUserNotificationSettings:settings];
        [self createLocalNotification];
        
    }else {
        //如果为8.0以下,用之前方法去注册
        [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound)];
    }

    
    [self.window makeKeyAndVisible];
    return YES;
}

// 判断引导图是否存在,不存在就创建
- (void)judgeExistOfFile {
    
    NSString *file = [self setFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
        [self enterMainInterface];
    }else {
        self.guideView = [[APPGuidView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        self.guideView.delegate = self;
        [self.window addSubview:self.guideView];
    }
    [self.guideView.button addTarget:self action:@selector(enterMainInterface) forControlEvents:UIControlEventTouchUpInside];
}

// 添加UIPageControl
- (void)addPageControlForGuideView {
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((kWidth - 150)/2, kHeight - 40, 150, 30)];
    self.pageControl.numberOfPages = 3;
    self.pageControl.pageIndicatorTintColor = [UIColor cyanColor];
    self.pageControl.currentPageIndicatorTintColor = kRGB(250.0, 180.0, 255.0);
    [self.window addSubview:self.pageControl];
    
    
    
}

//创建本地推送
- (void)createLocalNotification {
    
    // 创建一个本地推送
    
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    //设置每天10点推送消息之后
    
   NSDate *pushDate = [NSDate dateWithTimeIntervalSince1970:2 * 60 * 60];
  //  NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
    
    if (notification != nil) {
        
        // 设置推送时间
        
        
        notification.fireDate = pushDate;
        
        
        // 设置时区
        
        
        notification.timeZone = [NSTimeZone defaultTimeZone];
        
        
        // 设置重复间隔
        
        
        notification.repeatInterval = kCFCalendarUnitDay;
        
        
        // 推送声音
        
        
        notification.soundName = UILocalNotificationDefaultSoundName;
        
        
        // 推送内容
        
        
        notification.alertBody = @"优礼内容更新了,进来看看吧!";
        
        
        //显示在icon上的红色圈中的数子
        
        
        notification.applicationIconBadgeNumber = 1;
        
        
        //设置userinfo 方便在之后需要撤销的时候使用
        
        
        NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
        
        
        notification.userInfo = info;
        
        
        //添加推送到UIApplication
        
        
        UIApplication *app = [UIApplication sharedApplication];
        
        
        [app scheduleLocalNotification:notification];
        
    }
    
    
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    
    //点击提示框的打开
    application.applicationIconBadgeNumber = 0;
    
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    //当程序还在后天运行
    application.applicationIconBadgeNumber = 0;
}




// 设置沙盒路径
- (NSString *)setFilePath {
    
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *file = [path stringByAppendingString:@"APP.txt"];
    return file;
}

- (void)enterMainInterface {
    
    NSString *file = [self setFilePath];
    if (![[NSFileManager defaultManager] fileExistsAtPath:file]) {
        [[NSFileManager defaultManager] createFileAtPath:file contents:nil attributes:nil];
    }
    [self.guideView removeFromSuperview];
    BaseTabBarViewController *baseTVC = [[BaseTabBarViewController alloc] init];
    self.window.rootViewController = baseTVC;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = self.guideView.contentOffset.x / kWidth;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}



- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
